package com.example.chapter7;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		final EditText idEditText = (EditText) this.findViewById(R.id.main_id);
		final EditText pwEditText = (EditText) this.findViewById(R.id.main_pw);
		final TextView resultTextView = (TextView) this.findViewById(R.id.main_textview);
		
		Button button = (Button) this.findViewById(R.id.login_button);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				Thread thread = new Thread(new Runnable() {
//					@Override
//					public void run() {
//						login(idEditText, pwEditText);
//					}
//				});
//				thread.start();
				login(idEditText, pwEditText);
			}

			private void login(final EditText idEditText,
					final EditText pwEditText) {
				
				AsyncTask<Void, Integer, String> task = new AsyncTask<Void, Integer, String>() {

					@Override
					protected String doInBackground(Void... params) {
						try {
						String url = "https://logins.daum.net/accounts/mobile.do?url=(*)";
						List<NameValuePair> p = new ArrayList<NameValuePair>();
						p.add(new BasicNameValuePair("id", idEditText.getText().toString()));
						p.add(new BasicNameValuePair("pw", pwEditText.getText().toString()));
						DefaultHttpClient client = new DefaultHttpClient();
						HttpPost request = new HttpPost(url);
						
							HttpEntity entity = new UrlEncodedFormEntity(p, "UTF-8");
							request.setEntity(entity);
							
							HttpResponse response = client.execute(request);
							final int statusCode = response.getStatusLine().getStatusCode();
							
							Log.d("TEST", "code is " + statusCode);
							
							return "code is " + statusCode;
						} catch (Exception e) {
							Log.e("TEST", "error", e);
						}
						
						return null;
					}

					@Override
					protected void onPostExecute(String result) {
						super.onPostExecute(result);
						if (result == null) {
							result = "fail";
						}
						resultTextView.setText(result);
					}
				};
				task.execute();
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}